//
//  VSFindGiftViewController.m
//  UI项目2.0
//
//  Created by dlios on 15/7/15.
//  Copyright (c) 2015年 星际科技. All rights reserved.
//

#import "VSFindGiftViewController.h"
#import "VSFindGiftWaterLayout.h"
#import "VSAFNetworkHandler.h"
#import "VSFindGiftCollectionViewCell.h"
#import "VSFrameGrand.h"
#import "VSFindGiftModel.h"
#import "VSFindGiftPushCollectionViewController.h"
#import "MJRefresh.h"
@interface VSFindGiftViewController () <UICollectionViewDataSource, UICollectionViewDelegate, WaterLayoutDelegate>

@property (nonatomic, retain) NSMutableArray *GiftArr;
@property (nonatomic, retain) UICollectionView *GiftCollectionView;
@property (nonatomic, retain) VSFindGiftModel *GiftModel;
@property (nonatomic, assign) NSInteger GiftIndex;

@end

@implementation VSFindGiftViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    self.GiftIndex = 24;
    self.GiftArr = [NSMutableArray array];
    [self GiftgetDataDown];
    if (self.GiftCollectionView != nil) {
        [self GiftGetData];
    }
    
}

- (void)createView {
    VSFindGiftWaterLayout *layout = [[[VSFindGiftWaterLayout alloc] init] autorelease];
    
    layout.sectionInset = UIEdgeInsetsMake(0.040 * SCREEN_WIDTH, 10, 10, 10);
    layout.verticalSpacing = 10.0;
    layout.horizontalSpacing = 10.0;
    layout.lineCount = 2;
    
    self.GiftCollectionView = [[[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout] autorelease];
    
    self.GiftCollectionView.backgroundColor = [UIColor whiteColor];
    [self.GiftCollectionView registerClass:[VSFindGiftCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    self.GiftCollectionView.delegate = self;
    self.GiftCollectionView.dataSource = self;
    [self.view addSubview:self.GiftCollectionView];
    
    if (self.GiftArr != nil) {
        self.GiftCollectionView.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(GiftGetData)];
    }
    self.GiftCollectionView.header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(GiftgetDataDown)];
    
    
    UIImageView *backGroundImage = [[UIImageView alloc]initWithFrame:self.view.bounds];
    backGroundImage.image = [UIImage imageNamed:@"background001.png"];
    self.GiftCollectionView.backgroundView = backGroundImage;
}

- (void)GiftgetDataDown
{
    NSString *str1 = @"http://www.duitang.com/napi/index/hot/?include_fields=sender,album,icon_url,like_count,reply_count&__dtac={\"_r\": \"797606\"}&start=0&limit=24";
    
    [VSAFNetworkHandler GETwithAFNbyURL:str1 completion:^(id result) {
        [self.GiftArr removeAllObjects];
     NSArray *tempArr = [[result objectForKey:@"data"]objectForKey:@"object_list"];
        for (NSDictionary *dic in tempArr) {
            self.GiftModel = [VSFindGiftModel baseModelWithDic:dic];
            [self.GiftArr addObject:self.GiftModel];
        }
        [self createView];
        [self.GiftCollectionView reloadData];
        [self.GiftCollectionView.header endRefreshing];
    }];

}

- (void)GiftGetData
{
    
   
    
    /*
    http://www.duitang.com/napi/index/hot/?include_fields=sender%2Calbum%2Cicon_url%2Clike_count%2Creply_count&__dtac=%257B%2522_r%2522%253A%2520%2522797606%2522%257D&start=72&limit=24
    http://www.duitang.com/napi/index/hot/?include_fields=sender%2Calbum%2Cicon_url%2Clike_count%2Creply_count&__dtac=%257B%2522_r%2522%253A%2520%2522797606%2522%257D&start=96&limit=24
    http://www.duitang.com/napi/index/hot/?include_fields=sender%2Calbum%2Cicon_url%2Clike_count%2Creply_count&__dtac=%257B%2522_r%2522%253A%2520%2522797606%2522%257D&start=120&limit=24
    */
    
    NSString *str1 = [[@"http://www.duitang.com/napi/index/hot/?include_fields=sender,album,icon_url,like_count,reply_count&__dtac={\"_r\": \"797606\"}&start=" stringByAppendingString:[NSString stringWithFormat:@"%ld", self.GiftIndex]]stringByAppendingString:@"&limit=24"];
    
    [VSAFNetworkHandler GETwithAFNbyURL:str1 completion:^(id result) {
        NSArray *tempArr = [[result objectForKey:@"data"]objectForKey:@"object_list"];
        for (NSDictionary *dic in tempArr) {
            self.GiftModel = [VSFindGiftModel baseModelWithDic:dic];
            [self.GiftArr addObject:self.GiftModel];
            
        }
        if (self.GiftCollectionView == nil) {
             [self createView];
        }
        self.GiftIndex += 24;
        [self.GiftCollectionView reloadData];
        [self.GiftCollectionView.footer endRefreshing];
    }];
    
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.GiftArr.count;
}

- ( UICollectionViewCell *)collectionView:( UICollectionView *)collectionView cellForItemAtIndexPath:( NSIndexPath *)indexPath
{
    VSFindGiftCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    cell.findModel = self.GiftArr[indexPath.item];
    return cell;
}

- (CGSize)collectionView:( UICollectionView *)collectionView layout:( UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:( NSIndexPath *)indexPath {
    VSFindGiftModel *model = self.GiftArr[indexPath.item];
    return CGSizeMake((SCREEN_WIDTH - 30) / 2,  (SCREEN_WIDTH - 30) / 2 / [[model.photo objectForKey:@"width"] floatValue] *[[model.photo objectForKey:@"height"] floatValue] + [[self class] heightWithText:model.msg] + 8 + 23);
}



+ (CGFloat)heightWithText:(NSString *)text
{
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:15]};
    CGSize size = CGSizeMake((SCREEN_WIDTH - 30) / 2, 50);
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    return rect.size.height;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    VSFindGiftPushCollectionViewController *new = [[[VSFindGiftPushCollectionViewController alloc]init]autorelease];
    new.giftModel = self.GiftArr[indexPath.item];
    [self.navigationController pushViewController:new animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
